/*
 * $$Id: BaseAction.java,v 1.17 2004/06/28 20:26:29 bel70 Exp $$
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License
 * at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and
 * limitations under the License.
 *
 * The Original Code is jforum forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *              Dmitry Belov <bel@jresearch.org>
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Created on 09.05.2003
 *
 */
package com.jred.jforum.actions;

import java.util.List;

import org.apache.commons.logging.Log;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jred.jforum.exception.JForumException;
import com.jred.jforum.log.ForumLog;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * DOCUMENT ME!
 * 
 * @author Bel
 */
public abstract class BaseAction extends Action {
	private int accessLevel;
	public Log forumLogger = ForumLog.getInstance().getForumLogger();

	protected static WebApplicationContext ctx;

	/**
	 * setServlet������ֲ��Spring's ApplicationContext
	 * 
	 * @param actionServlet
	 * @return
	 */
	public void setServlet(ActionServlet actionServlet) {
		super.setServlet(actionServlet);
		ServletContext servletContext = actionServlet.getServletContext();
		ctx = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
	}

	/**
	 * getCtx������ȡ��Spring's ApplicationContext
	 * 
	 * @return WebApplicationContext
	 */
	public static WebApplicationContext getCtx() {
		return ctx;
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param request
	 *            DOCUMENT ME!
	 * @param mess
	 *            DOCUMENT ME!
	 * @param append
	 *            DOCUMENT ME!
	 */
	public void log(HttpServletRequest request, String mess, String append) {
		HttpSession session = request.getSession();
		// User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
		MessageResources messages = getResources(request);

		StringBuffer sb = new StringBuffer();
		sb.append(request.getRemoteAddr());
		sb.append(" User:");
		// sb.append((user.getName() == null) ? "<anonimous>" : user.getName());
		sb.append(" ");
		sb.append(messages.getMessage(mess));
		sb.append(" ");
		sb.append(append);
		forumLogger.info(sb.toString());
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param session
	 *            DOCUMENT ME!
	 * @param key
	 *            DOCUMENT ME!
	 * @param arg0
	 *            DOCUMENT ME!
	 * @param arg1
	 *            DOCUMENT ME!
	 */
	// public void setStatusMessage(HttpServletRequest request, String key,
	// String arg0,
	// String arg1) {
	//        	
	// MessageResources messages=getResources(request);
	// String message=
	// messages.getMessage(super.getLocale(request),key,arg0,arg1);
	// request.getSession().setAttribute(IConst.SESSION.STATUS_MESSAGE,
	// message);
	// }
	/**
	 * DOCUMENT ME!
	 * 
	 * @param session
	 *            DOCUMENT ME!
	 * @param key
	 *            DOCUMENT ME!
	 * @param arg0
	 *            DOCUMENT ME!
	 */
	// public void setStatusMessage(HttpServletRequest request, String key,
	// String arg0) {
	// setStatusMessage(request, key, arg0, "");
	// }
	/**
	 * DOCUMENT ME!
	 * 
	 * @param session
	 *            DOCUMENT ME!
	 * @param key
	 *            DOCUMENT ME!
	 */
	// public void setStatusMessage(HttpServletRequest request, String key) {
	// setStatusMessage(request, key, "");
	// }
	/**
	 * DOCUMENT ME!
	 * 
	 * @param request
	 *            DOCUMENT ME!
	 * @param mess
	 *            DOCUMENT ME!
	 */
	public void log(HttpServletRequest request, String mess) {
		log(request, mess, "");
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param request
	 *            DOCUMENT ME!
	 * 
	 * @return DOCUMENT ME!
	 */
	// public boolean haveAccess(HttpServletRequest request) {
	// HttpSession session = request.getSession();
	// User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
	// boolean access = (user.getStatus() >= this.accessLevel);
	//
	// if (!access) {
	// ForumLog.getInstance().getForumLogger().warn(request.getRemoteAddr() +
	// " " + user.getName() + " " +
	// getResources(request).getMessage("logs.LOG27") + " " +
	// request.getRequestURI());
	// }
	//
	// return access;
	// }

	/**
	 * DOCUMENT ME!
	 * 
	 * @param mapping
	 *            DOCUMENT ME!
	 * @param form
	 *            DOCUMENT ME!
	 * @param request
	 *            DOCUMENT ME!
	 * @param response
	 *            DOCUMENT ME!
	 * 
	 * @return DOCUMENT ME!
	 * 
	 * @throws Exception
	 *             DOCUMENT ME!
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		MessageResources messages = getResources(request);

		// if (!haveAccess(request)) {
		// return (mapping.findForward("jforum-denied"));
		// }

		ActionForward forward = this.process(mapping, form, request, response);
		afterProcess(mapping, form, request, response);

		// request.setAttribute(IConst.REQUEST.REQUEST_URI,
		// request.getRequestURI());
		//
		// ForumDAO forumDAO = ForumDAO.getInstance();
		//
		// if (session.getAttribute(IConst.SESSION.LAST_UPDATE_DATE) == null) {
		// session.setAttribute(IConst.SESSION.LAST_UPDATE_DATE, new Date());
		// }
		//
		// try {
		// forumDAO.setDataSource(getDataSource(request));
		//
		// EntryList elist = new EntryList();
		// forumDAO.fillEntryList(elist);
		// request.setAttribute(IConst.REQUEST.ENTRY_LIST, elist);
		//
		// if ((session.getAttribute(IConst.SESSION.GROUPS_KEY) == null) ||
		// (((Date) session.getAttribute(
		// IConst.SESSION.LAST_UPDATE_DATE)).before(
		// (Date)
		// getServlet().getServletContext().getAttribute(IConst.CONTEXT.LAST_UPDATE_DATE))))
		// {
		// User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
		// session.setAttribute(IConst.SESSION.GROUPS_KEY,
		// forumDAO.getGroups(user.getStatus(), false));
		// session.setAttribute(IConst.SESSION.LAST_UPDATE_DATE, new Date());
		// }
		// } catch (SQLException sqle) {
		// getServlet().log("Connection.process", sqle);
		// throw new SystemException(sqle);
		// }

		return (forward);
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param mapping
	 *            DOCUMENT ME!
	 * @param form
	 *            DOCUMENT ME!
	 * @param request
	 *            DOCUMENT ME!
	 * @param response
	 *            DOCUMENT ME!
	 */
	public void afterProcess(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param mapping
	 *            DOCUMENT ME!
	 * @param form
	 *            DOCUMENT ME!
	 * @param request
	 *            DOCUMENT ME!
	 * @param response
	 *            DOCUMENT ME!
	 * 
	 * @return DOCUMENT ME!
	 * @throws SystemException
	 */
	public abstract ActionForward process(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws JForumException;

	/**
	 * DOCUMENT ME!
	 * 
	 * @return
	 */
	public int getAccessLevel() {
		return accessLevel;
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param i
	 */
	public void setAccessLevel(int i) {
		accessLevel = i;
	}
}
